package com.liyue.permission;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;

import androidx.annotation.NonNull;

import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;
import com.yanzhenjie.permission.PermissionListener;
import com.yanzhenjie.permission.Rationale;
import com.yanzhenjie.permission.RationaleListener;

import java.util.List;

/**
 * Created by Ly
 * on 2021/7/6
 *
 * 相机权限检测授予
 */
public class checkPermission {

    public static Activity mActivity = null;

    public static void checkPermission(Activity activity) {
        mActivity = activity;
        //先判断是否有相机权限
        AndPermission.with(mActivity)
                .requestCode(100)
                .permission(Permission.CAMERA)
                .callback(listener)
                .rationale(mRationaleListener)
                .start();
    }

    private static RationaleListener mRationaleListener = new RationaleListener() {
        @Override
        public void showRequestPermissionRationale(int requestCode, final Rationale rationale) {
            if (100 == requestCode) {
                Log.i("lgq", "拒绝了。。。");
            }
        }
    };
    //相机权限监听
    static PermissionListener listener = new PermissionListener() {
        @Override
        public void onSucceed(int requestCode, @NonNull List<String> grantPermissions) {
            if (100 == requestCode) {//打开摄像头
                Log.i("lgq", "同意了了。。。");
            }
        }

        @Override
        public void onFailed(int requestCode, @NonNull List<String> deniedPermissions) {

            if (100 == requestCode) {//相机
                AndPermission.defaultSettingDialog(mActivity, 100)
                        .setTitle("权限申请失败")
                        .setMessage("需相机基本权限,否则您将无法正常使用，请在设置中授权")
                        .setPositiveButton("好，去设置")
                        .setNegativeButton("", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                return;
                            }
                        })
                        .show();
            }

        }
    };

}
